package com.iims.base.db.generator;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.util.CollectionUtils;

import javax.sql.DataSource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author ysw
 * @Dcscription:代码生成类
 * @Date 2020/3/12 0012 15:37
 */
public class MysqlGenerator {
    private static Logger logger = LoggerFactory.getLogger(MysqlGenerator.class);

    private static final String database = "zytm_iims";
    private static final String url = "jdbc:mysql://localhost:3306/" + database
            + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private static final String driverName = "com.mysql.cj.jdbc.Driver";
    private static final String userName = "zytm";
    private static final String password = "zytm";
    private static String basePath = "";
    private static String mapperPath = "";
    private static String mapperXMLPath = "";

    public static void main(String[] args) {
        generate("fanpei", "com.iims.framework.db", "");
    }

    /**
     * @param
     * @Description:自动生成代码
     * @Author: ysw
     * @Return void
     * @Date: 2020/03/12 15:42
     */
    public static void generate(String author, String packageName, String... tablePrefix) {
        // 全局配置
        GlobalConfig gc = initGlobalConfig(author, packageName);
        // 数据源配置
        DataSourceConfig dsc = initDataSourceConfig();
        // 包配置，设置生成的文件放到哪个包下面
        PackageConfig pc = new PackageConfig()
                .setParent(packageName);
        //.setController("controller");
        // 模板引擎配置
        FreemarkerTemplateEngine templateEngine = new FreemarkerTemplateEngine();
        //每一个entity都需要单独设置InjectionConfig, StrategyConfig和TemplateConfig
        Map<String, String> names = new JdbcRepository().getEntityNames(tablePrefix);
        if (MapUtils.isEmpty(names)) {
            return;
        }
        for (String tableName : names.keySet()) {
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();
            mpg.setGlobalConfig(gc);
            mpg.setDataSource(dsc);
            mpg.setPackageInfo(pc);
            mpg.setTemplateEngine(templateEngine);
            // 自定义配置
            InjectionConfig cfg = initInjectionConfig(packageName);
            mpg.setCfg(cfg);
            // 策略配置
            StrategyConfig strategy = initStrategyConfig(tableName, tablePrefix);
            mpg.setStrategy(strategy);
            // 模板配置
            // mapper文件
            String mapperFile = mapperPath
                    + "/" + names.get(tableName) + "Mapper.java";
            TemplateConfig tc = initTemplateConfig(mapperFile);
            mpg.setTemplate(tc);
            //开始执行
            mpg.execute();
        }
    }

    /**
     * @param
     * @Description:配置数据源
     * @Author: ysw
     * @Return com.baomidou.mybatisplus.generator.config.DataSourceConfig
     * @Date: 2020/03/12 16:22
     */
    private static DataSourceConfig initDataSourceConfig() {
        return new DataSourceConfig()
                .setUrl(url)
                .setDriverName(driverName)
                .setUsername(userName)
                .setPassword(password);
    }

    /**
     * @param author
     * @Description:全局配置
     * @Author: ysw
     * @Return com.baomidou.mybatisplus.generator.config.GlobalConfig
     * @Date: 2020/03/12 16:27
     */
    private static GlobalConfig initGlobalConfig(String author, String packageName) {
        GlobalConfig gc = new GlobalConfig();
        String tmp = MysqlGenerator.class.getResource("").getPath();
        String codeDir = StringUtils.truncate(tmp, tmp.indexOf("/target"));
        basePath = codeDir + "/src/main/java";
        mapperPath = basePath + "/" + packageName.replace(".", "/") + "/mapper";
        mapperXMLPath = codeDir + "/src/main/resources/mapper";
        System.out.println("basePath = " + basePath + "\nmapperPath = " + mapperPath);
        gc.setOutputDir(basePath);
        gc.setAuthor(author);
        gc.setOpen(false);
        gc.setServiceName("%sService");
        gc.setFileOverride(true);
        return gc;
    }

    /**
     * @param packageName
     * @Description:自定义配置
     * @Author: ysw
     * @Return com.baomidou.mybatisplus.generator.InjectionConfig
     * @Date: 2020/03/12 16:28
     */
    private static InjectionConfig initInjectionConfig(String packageName) {
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                //自定义输入文件名称
                return mapperXMLPath
                        + "/" + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    /**
     * @param tableName
     * @Description:策略配置
     * @Author: ysw
     * @Return com.baomidou.mybatisplus.generator.config.StrategyConfig
     * @Date: 2020/03/12 16:32
     */
    private static StrategyConfig initStrategyConfig(String tableName, String... tablePrefix) {
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setTablePrefix(tablePrefix);
        strategy.setInclude(tableName);
        strategy.setRestControllerStyle(true);
        return strategy;
    }

    /**
     * @param mapperFile
     * @Description:覆盖entity及xml
     * @Author: ysw
     * @Return com.baomidou.mybatisplus.generator.config.TemplateConfig
     * @Date: 2020/03/12 16:35
     */
    private static TemplateConfig initTemplateConfig(String mapperFile) {
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        templateConfig.setController(null);//不生成controller
        templateConfig.setService(null);
        templateConfig.setServiceImpl(null);
        //如果当前Entity已经存在,那么仅仅覆盖Entity
        File file = new File(mapperFile);
        if (file.exists()) {
            templateConfig.setController(null);
            templateConfig.setMapper(null);
            templateConfig.setService(null);
            templateConfig.setServiceImpl(null);
            templateConfig.setEntity(null);
        }
        return templateConfig;
    }

    public static class JdbcRepository {
        private static Pattern linePattern = Pattern.compile("_(\\w)");
        private JdbcOperations jdbcOperations;

        /**
         * @param
         * @Description:数据库连接池
         * @Author: ysw
         * @Return
         * @Date: 2020/03/12 17:38
         */
        public JdbcRepository() {
            DataSource dataSource = DataSourceBuilder.create()
                    //如果不指定类型，那么默认使用连接池，会存在连接不能回收而最终被耗尽的问题
                    .type(DriverManagerDataSource.class)
                    .driverClassName(driverName)
                    .url(url)
                    .username(userName)
                    .password(password)
                    .build();
            this.jdbcOperations = new JdbcTemplate(dataSource);
        }

        /**
         * @param tablePrefixArray
         * @Description:获取所有实体类的名字,实体类由数据库表名转换而来,例如: 表前缀为auth, 完整表名为auth_first_second, 那么entity则为FirstSecond
         * @Author: ysw
         * @Return java.util.Map<java.lang.String, java.lang.String>
         * @Date: 2020/03/12 17:38
         */
        public Map<String, String> getEntityNames(String... tablePrefixArray) {
            //该sql语句目前支持mysql
            String sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '" + database + "'";
            if (tablePrefixArray != null && tablePrefixArray.length != 0) {
                if (tablePrefixArray.length > 1) {
                    sql += "and ( ";
                    for (String prefix : tablePrefixArray) {
                        sql += " table_name like '" + prefix + "_%' or ";
                    }
                    sql += ")";
                    sql = StrUtil.replace(sql, sql.lastIndexOf("or"), sql.lastIndexOf("or") + 3, ' ');
                } else {
                    sql += " and table_name like '" + tablePrefixArray[0] + "_%'";
                }
            }
            List<String> tableNames = jdbcOperations.query(sql, SingleColumnRowMapper.newInstance(String.class));
            if (CollectionUtils.isEmpty(tableNames)) {
                return MapUtil.newHashMap();
            }
            Map<String, String> result = MapUtil.newHashMap();
            tableNames.forEach(
                    tableName -> {
                        String entityName = underlineToCamel(tableName);
                        if (tablePrefixArray != null && tablePrefixArray.length != 0) {
                            for (String prefix : tablePrefixArray) {
                                //如果有前缀,需要去掉前缀
                                if (tableName.startsWith(prefix)) {
                                    String tableNameRemovePrefix = tableName.substring((prefix + "_").length());
                                    entityName = underlineToCamel(tableNameRemovePrefix);
                                }
                            }
                        }
                        result.put(tableName, entityName);
                    }
            );
            return result;
        }

        /**
         * @param str
         * @Description:下划线转驼峰
         * @Author: ysw
         * @Return java.lang.String
         * @Date: 2020/03/12 17:37
         */
        private static String underlineToCamel(String str) {
            if (StringUtils.isBlank(str)) {
                return str;
            }
            str = str.toLowerCase();
            Matcher matcher = linePattern.matcher(str);
            StringBuffer sb = new StringBuffer();
            while (matcher.find()) {
                matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
            }
            matcher.appendTail(sb);
            str = sb.toString();
            str = str.substring(0, 1).toUpperCase() + str.substring(1);
            return str;
        }
    }
}